<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="盘点单号" prop="inventoryCheckNo">
        <el-input
          v-model="queryParams.inventoryCheckNo"
          placeholder="请输入库存盘点单号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="盘点状态" prop="inventoryCheckStatus">
        <el-radio-group v-model="queryParams.inventoryCheckStatus" @change="handleQuery">
            <el-radio-button :key="-2" :label="-2">
              全部
            </el-radio-button>
            <el-radio-button v-for="item in inventory_check_status" :key="item.value" :label="item.value">
              {{ item.label }}
            </el-radio-button>
          </el-radio-group>
      </el-form-item>  
      <el-form-item label="仓库" prop="warehouseId">
                  <el-select @click="selectware" v-model="ware" placeholder="请选择仓库">
                    <el-option @click="seachware(dict.warehouseId)" v-for="dict in options"
                      :key="'key-' + dict.warehouseId" :label="dict.warehouseName"
                      :value="dict.warehouseId"></el-option>
                  </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- v-hasPermi="['wms:check:add']" -->
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
         
        >新增盘点单</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="checkList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="库存盘点单号" align="center" prop="inventoryCheckNo" />
      <el-table-column label="赢亏数" align="center" prop="inventoryCheckTotal" />
      <el-table-column label="仓库" align="center" width="200" prop="warehouseName"/>
      
      <el-table-column label="库存盘点单状态：盘点中 已完成" align="center" prop="inventoryCheckStatus">
          <template #default='scope'>
            <dict-tag :options="inventory_check_status" :value="scope.row.inventoryCheckStatus" />
          </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <router-link :to="'/wms/CheckDetail/index?inventoryCheckNo=' + scope.row.inventoryCheckNo" class="link-type">
            <span>查看盘点</span>
          </router-link>
          <!-- <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['wms:check:edit']">继续盘点</el-button> -->
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['wms:check:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改库存盘点单据对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="checkRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="库存盘点单号，系统自动生成" prop="inventoryCheckNo">
          <el-input v-model="form.inventoryCheckNo" placeholder="请输入库存盘点单号，系统自动生成" />
        </el-form-item>
        <el-form-item label="盈亏数" prop="inventoryCheckTotal">
          <el-input v-model="form.inventoryCheckTotal" placeholder="请输入盈亏数" />
        </el-form-item>
        <el-form-item label="审核人" prop="checkUserId">
          <el-input v-model="form.checkUserId" placeholder="请输入审核人" />
        </el-form-item>
        <el-form-item label="审核时间" prop="checkTime">
          <el-date-picker clearable
            v-model="form.checkTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择审核时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="所属仓库" prop="warehouseId">
          <el-input v-model="form.warehouseId" placeholder="请输入所属仓库" />
        </el-form-item>
        <el-form-item label="所属库区" prop="areaId">
          <el-input v-model="form.areaId" placeholder="请输入所属库区" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="删除标识" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标识" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Check">
import { listCheck, getCheck, delCheck, addCheck, updateCheck } from "@/api/wms/check";
import { listWarehouse } from '@/api/wms/warehouse'
const { proxy } = getCurrentInstance();
const { inventory_check_status} = proxy.useDict("inventory_check_status");
const checkList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    inventoryCheckNo: null,
    inventoryCheckType: null,
    inventoryCheckStatus: null,
    inventoryCheckTotal: null,
    checkStatus: null,
    checkUserId: null,
    checkTime: null,
    warehouseId: null,
    areaId: null,
  },
  rules: {
    delFlag: [
      { required: true, message: "删除标识不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);


//查询仓库
function selectware() {
  listWarehouse().then(response => {
    console.log(response);
    options.value = response.rows
  })
}
//查询库区
function seachware(id) {
  selectList(id).then(response => {
    area.value = null
    areas.value = response.rows
  })
}
/** 查询库存盘点单据列表 */
function getList() {
  loading.value = true;
  listCheck().then(response => {
    checkList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    inventoryCheckNo: null,
    inventoryCheckType: null,
    inventoryCheckStatus: null,
    inventoryCheckTotal: null,
    checkStatus: null,
    checkUserId: null,
    checkTime: null,
    warehouseId: null,
    areaId: null,
    remark: null,
    delFlag: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  proxy.resetForm("checkRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  proxy.$router.push({ name:"Checkadd",path: "/wms/Checkadd/index" });
}

/** 修改按钮操作 */
function handleUpdate(row) {
  // reset();
  // const _id = row.id || ids.value
  // getCheck(_id).then(response => {
  //   form.value = response.data;
  //   open.value = true;
  //   title.value = "修改库存盘点单据";
  // });
  proxy.$router.push({ name:"CheckDetail",path: "/wms/CheckDetail/index" ,query:{id:row.id,a:"1"}});
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["checkRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateCheck(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addCheck(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除库存盘点单据编号为"' + _ids + '"的数据项？').then(function() {
    return delCheck(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('wms/check/export', {
    ...queryParams.value
  }, `check_${new Date().getTime()}.xlsx`)
}

getList();
</script>
